package com.kaiyidiannao.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandHouse;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mark on 14-4-11.
 */
public class SecondHandHouseAdapter extends ImageLoaderBaseAdapter {
    private ImageLoader imageLoader;
    private ListView secondHandHouseList;
    ArrayList<UserBean> userBeans = DataCenter.userLogin;
    private LayoutInflater layoutInflater;
    private List<SecondHandHouse> list = new ArrayList<SecondHandHouse>();
    private int typeid = DataCenter.typeid;
    private Context context;

    public SecondHandHouseAdapter(Context context, List<SecondHandHouse> list) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        TextView nameTextView = null;
        TextView priceTextView = null;
        TextView squareTextView = null;
        TextView ageLimitTextView = null;
        TextView callNameTextView = null;
        TextView popularityTextView = null;
        TextView publishTime;
        TextView textPrice = null;
        ImageButton callNum = null;
        TextView callName = null;
        LinearLayout call;
        ImageButton btDelete = null;
        TextView textPop = null;
        final SecondHandHouse secondHandHouse = list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.secondhandhouse_item, null);
        }
        textPop = (TextView) convertView.findViewById(R.id.textPop);
        btDelete = (ImageButton) convertView.findViewById(R.id.btDelete);
        callNum = (ImageButton) convertView.findViewById(R.id.callNum);
        textPrice = (TextView) convertView.findViewById(R.id.textPrice);
        imageView = (ImageView) convertView.findViewById(R.id.secondHandHouseImage);
        nameTextView = (TextView) convertView.findViewById(R.id.secondHandHouseName);
        priceTextView = (TextView) convertView.findViewById(R.id.secondHandHousePrice);
        squareTextView = (TextView) convertView.findViewById(R.id.square);
        ageLimitTextView = (TextView) convertView.findViewById(R.id.ageLimit);
        callNameTextView = (TextView) convertView.findViewById(R.id.callName);
        popularityTextView = (TextView) convertView.findViewById(R.id.popularity);
        publishTime = (TextView)convertView.findViewById(R.id.publishTime);
        call = (LinearLayout)convertView.findViewById(R.id.call);

        btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeid == 65) {
                    String action = "delete";
                    DataCenter.position = position;
                    Log.d("secondHandHouseAdapter", secondHandHouse.getFid() + "  " + position + "");
                    ((BaseActionBarActivity) context).showProgressDialog();
                    new PostDeleteMessageInfo(secondHandHouse.getFid(), userBeans.get(0).getUserid(), action).execute();
                }
            }
        });
        imageLoader.displayImage(secondHandHouse.getThumb(), imageView, options, null);


        nameTextView.setText(secondHandHouse.getTitle());
        if (secondHandHouse.getPrice() == null || secondHandHouse.getPrice().equals("")) {
            textPrice.setVisibility(View.GONE);
            priceTextView.setVisibility(View.GONE);
        } else {
            priceTextView.setText(secondHandHouse.getPrice() + "万");
        }

        if (secondHandHouse.getArea() == null || secondHandHouse.getArea().equals("")) {
            squareTextView.setVisibility(View.GONE);
        } else {
            squareTextView.setText(secondHandHouse.getArea() + "㎡");
        }

        Date date = new Date();

        if (secondHandHouse.getHouseyear() == null || secondHandHouse.getHouseyear().equals("")) {
            ageLimitTextView.setVisibility(View.GONE);
        } else {
            ageLimitTextView.setText(secondHandHouse.getHouseyear() + "年");
        }
        callNameTextView.setText(secondHandHouse.getUsername());
        if (secondHandHouse.getHits() == null || secondHandHouse.getHits().equals("")) {
            popularityTextView.setVisibility(View.GONE);
            textPop.setVisibility(View.GONE);
        } else {
            popularityTextView.setText(secondHandHouse.getHits());
        }

//        publishTime.setText(secondHandHouse.getAddtime());
        if (null != call){
            call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phoneNum =  secondHandHouse.getTelephone();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + phoneNum));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }
}
